		
	
capture cd D:\Dropbox\book_welfare\replication



	***************************************************************************
	* make X: create a datafile that the rest of the program can access 
	***************************************************************************
	
	
use  "data\books_year.dta", clear 
		keep if year==2016
		keep if pubyr>=1960 & pubyr<=2016 

		mvencode fbookp, mv(0) o 

		merge m:1 pubyr using replication\data\Nfcap_gr.dta
		gen double M = 10.00e+07



		gen male=1-fbookp
		egen Nmale=sum(male), by(pubyr)


		gen double s = q/M 
		egen Q=sum(q) 
		gen double s0 = (M-Q)/M	

		egen QF=sum(qf)
		egen QM=sum(qm)

		gen   MF = (QF/(QM+QF))*M
		gen double sf = qf/MF 
		gen double s0f = (MF-QF)/MF

		gen   MM = (QM/(QM+QF))*M
		gen double sm = qm/MM 
		gen double s0m = (MM-QM)/MM


		egen xA= sum(q) if pubyr<year, by(author_id)
		egen A=max(xA), by(author_id)

		mvencode A, mv(0) 

		gen lA = ln(A+1)
		gen dmiss=A==0 

		gen dfemale=fbookp

		capture keep M q Q fbookp s0 s   MM qm QM s0m sm   MF qf QF s0f sf  pubyr Nmale Nfcap genre lA dmiss   dfemale
		gen p = 10 

		keep if q~=. 
		gen double eliminate=.


		keep s s0 q Q fbookp  pubyr Nfcap genre lA dmiss   s0m sm s0f sf dfemale p
save data\X.dta, replace 
*********************************************


*********************************************
*********************************************
clear 
set obs 1 
gen v1=.
save data\collect.dta, replace
*********************************************
*********************************************

* loop through a range of sigmas for the results 
  foreach sigma in  .25 .373 .5 .75  {
		use data\X.dta, clear 
		egen gno =group(genre)
		 
		 gen sigma = `sigma'
		* gen sigma = 0.373 
		
		gen double delta = log(s) - log(s0) - sigma*log(q/Q)
		gen double deltam = ln(sm) - ln(s0m) - sigma*ln(sm/(1-s0m))
		gen double deltaf = ln(sf) - ln(s0f) - sigma*ln(sf/(1-s0f))
	
	
		* predictive model: expected quality based on X's	
		 reg delta  lA i.gno dmiss fbookp 
		 predict dh 
		 gen res=delta-dh 
		 gen res_sq = res^2 
		 gen sighat = res_sq^.5 
		 
		 
		 * parameterize (deltahat and sigma by book gender)
		 
		 gsort fbookp -dh 
		 bysort fbookp: gen count =_n
		 bysort fbookp: gen COUNT =_N
		 
		******************************
		* approximate male slope near end 
		
		regress dh count if count/COUNT>.9 & fbookp==0
		estimates save clean_room\dh, replace 
		gen lsighat = ln(sighat)
		regress lsighat count if count/COUNT>.9 & fbookp==0
		estimates save clean_room\sighat, replace 
		
		
		* make a count variable overall
		drop count COUNT
		 gsort -dh 
		 gen count=_n 

		
		
		gsort fbookp pubyr  -dh 
		bysort fbookp pubyr: gen rank=_n 

		gen drop = (fbookp==1 & rank > Nfcap)
		gsort -dh 
		
		* initial cs overall, rev - overall and for M and W 
		preserve 
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))
			su CS
			local cs0=r(mean)
			
			su q0 if _n/_N>.95 
			local old = r(mean)
			
			gen s_in = [D^(1-sigma)]/[1+D^(1-sigma)]
			gen rev=p*q0
			egen REV = sum(rev)
			gen revf=p*q0*dfemale
			egen REVF = sum(revf)
			gen revm=p*q0*(1-dfemale)
			egen REVM = sum(revm)
			
			su REV
			local rev0=r(mean) 
			su REVF
			local rev0f=r(mean) 
			su REVM
			local rev0m=r(mean) 
			
		restore 
		
		
		* initial cs male 
		preserve 
			gen double M = 10.00e+07
			gen expdeltam = exp(deltam/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q0m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSm= log(1 + Dm^(1-sigma))
			su CSm
			local cs0m=r(mean)
		restore 
		
		* initial cs female  
		preserve 
			gen double M = 10.00e+07
			gen expdeltaf = exp(deltaf/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q0f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSf= log(1 + Df^(1-sigma))
			su CSf
			local cs0f=r(mean)
		restore 
		
		
		*create a local variable (N) showing the status quo number of male-authored books in the choice set 
		* local N=_N 
		su count if fbookp==0 
		local N = r(N)
		
		
		***********************************************************
		* create additional male products based on relationship
		* between delta hat and entry order 
		***********************************************************
	
		preserve 
			clear 
			set obs 2000000
			gen count=_n 
			
			estimates use clean_room\dh
				local a = _b[_cons]
				local b = _b[count]
			estimates use clean_room\sighat 
				local c = _b[_cons]
				local d = _b[count]

			
			
			gen xdelta = `a' + `b'*count 
			set seed 99 
			drawnorm xepsilon 
			
			gen epsilon = (exp(`c' + `d'*count))*xepsilon 
			
			gen delta =xdelta + epsilon 
			
			keep if _n>`N'
 
			keep delta count 
			gen extra=1 
			
			save data\A.dta, replace
		restore 
	

	
		local T=_N
		
		append using data\A.dta
		
		save data\C.dta, replace 
		
		
		*********** all: this module calculates the status quo entry threshold, `old' 
		
		use data\C.dta, clear 

		 replace sigma=`sigma' if sigma==. 
		
		 drop count 
		 gen count=_n 
		
		
			capture drop M 
			capture drop expdelta D q0 
			
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			gen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))

			
			su q0
			su q0 if _n>`T'-5001 & _n<`T' 
			local old = r(mean)

			

			*******************************************************************
			*********** remove the women, and add endogenous male products  
			*******************************************************************
		use data\C.dta, clear
		drop if drop==1 

			replace sigma=`sigma' if sigma==. 
			
			drop count 
			gen count=_n 
			

			* calculate marginal q for varying N 
			capture drop M 
			capture drop expdelta D  
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			gen D=sum(expdelta) 
			gen qmN = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			
			  
			capture drop q0 
			
			* figure out how many additional male products by equating expected marginal q with the old entry threshold 
			preserve 
				reg qmN count if count>500000
				gen nstar =  (`old' - _b[_cons])/_b[count]
				su nstar
				local nstar=r(mean)
			restore 
			
			
			
			* now, just keep the products entering in the counterfactual
			
			gen double CS= log(1 + D^(1-sigma))
			su CS if _n==int(`nstar')
			local csw = r(mean)
			drop D expdelta 

			drop if _n>int(`nstar')
			
			
			* calculate counterfactual CS 
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q1 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CSW= log(1 + D^(1-sigma))

			reg deltam delta 
			predict m 
			gen deltamm=deltam
			replace deltamm=m if deltamm==. & extra==1 
	
			gen expdeltam = exp(deltamm/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q1m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSWm= log(1 + Dm^(1-sigma))
		
			reg deltaf delta 
			predict f 
			gen deltaff=deltaf
			replace deltaff=f if deltaff==. & extra==1 

			gen expdeltaf = exp(deltaff/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q1f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSWf= log(1 + Df^(1-sigma))
			
		
			
			
			gen rev1 = p*q1 
			egen REV1 = sum(rev1)
			gen revm1 = p*q1*(1-dfemale) 
			egen REVM1 = sum(revm1)
			gen revf1 = p*q1*dfemale 
			egen REVF1 = sum(revf1)
			
			
			keep count CS* sigma REV*
			


			 drop CS 
		
		gen CS=`cs0'
		gen CSm = `cs0m'
		gen CSf = `cs0f'
		gen CSW_alt = `csw'
		
		gen REV0 = `rev0'
		gen REVm = `rev0m'
		gen REVf = `rev0f'
		
		
		keep CS CSf CSm CSW CSW_alt  CSWf CSWm sigma REV*
		keep if _n==1 
		append using data\collect.dta
		save data\collect.dta, replace 
			
	 }
	
				
	
capture cd D:\Dropbox\book_welfare\replication  



		
	use data\collect.dta, clear 
		gen csratio = 100*(CS/CSW -1)
		gen csfratio = 100*(CSf/CSWf -1)
		gen csmratio = 100*(CSm/CSWm -1)
		
	
		gen revratio = 100*(REV0/REV1 -1)
		gen revfratio = 100*(REVf/REVF1 -1)
		gen revmratio = 100*(REVm/REVM1 -1)

		sort sigma 
		format rev* cs* %9.2f
		br sigma cs* rev*


*************************************************
* no predictability, no male endogenous response 
*************************************************




*********************************************
*********************************************
clear 
set obs 1 
gen v1=.
save data\collect_upper.dta, replace
*********************************************
*********************************************
capture cd D:\Dropbox\book_welfare\replication
 
* loop through a range of sigmas for the results 
 foreach sigma in  .25 .373 .5 .75  {
		use data\X.dta, clear 
		egen gno =group(genre)
		 
		gen sigma = `sigma'
		
		gen double delta = log(s) - log(s0) - sigma*log(q/Q)
		gen double deltam = ln(sm) - ln(s0m) - sigma*ln(sm/(1-s0m))
		gen double deltaf = ln(sf) - ln(s0f) - sigma*ln(sf/(1-s0f))
	
	
		set seed 99 
		drawnorm dh 
		 gsort -dh 
		 gen count=_n 

		
		
		gsort fbookp pubyr  -dh 
		bysort fbookp pubyr: gen rank=_n 

		gen drop = (fbookp==1 & rank > Nfcap)
		gsort -dh 
		
		* initial cs overall, rev - overall and for M and W 
		preserve 
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))
			
			gen s_in = [D^(1-sigma)]/[1+D^(1-sigma)]
			gen rev=p*q0
			egen REV = sum(rev)
			gen revf=p*q0*dfemale
			egen REVF = sum(revf)
			gen revm=p*q0*(1-dfemale)
			egen REVM = sum(revm)
			
			
	
			gen expdeltam = exp(deltam/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q0m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSm= log(1 + Dm^(1-sigma))

			gen expdeltaf = exp(deltaf/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q0f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSf= log(1 + Df^(1-sigma))
			
			su CS* REV* 
			
			collapse (mean) CS* REV* sigma 
			gen regime = "status quo"
			append using data\collect_upper.dta
			save  data\collect_upper.dta, replace 

		restore 
		
		
		preserve 
			drop if drop==1 
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))
			
			gen s_in = [D^(1-sigma)]/[1+D^(1-sigma)]
			gen rev=p*q0
			egen REV = sum(rev)
			gen revf=p*q0*dfemale
			egen REVF = sum(revf)
			gen revm=p*q0*(1-dfemale)
			egen REVM = sum(revm)
			
			
	
			gen expdeltam = exp(deltam/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q0m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSm= log(1 + Dm^(1-sigma))

			gen expdeltaf = exp(deltaf/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q0f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSf= log(1 + Df^(1-sigma))
			
			su CS* REV* 
			collapse (mean) CS* REV* sigma 
			gen regime = "no influx"
			append using data\collect_upper.dta
			save  data\collect_upper.dta, replace 


		restore 
}


use data\collect_upper.dta, clear
	

		gsort sigma -regime 
		gen cs_ch = 100*(CS/CS[_n+1]-1)
		gen csm_ch = 100*(CSm/CSm[_n+1]-1)
		gen csf_ch = 100*(CSf/CSf[_n+1]-1)

		gen rev_ch = 100*(REV/REV[_n+1]-1)
		gen revf_ch = 100*(REVF/REVF[_n+1]-1)
		gen revm_ch = 100*(REVM/REVM[_n+1]-1)
		
		format rev* cs* %9.2f
	
	
		order sigma cs_ch csf_ch csm_ch rev_ch revf_ch revm_ch 
		sort sigma 
		br sigma *_ch  if regime=="status quo"
		
		